// pages/record/record.js

import request from "../../utils/request.js";
import local from "../../utils/local";
import nav from "../../utils/nav.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    appid: 'wx15a7538fa77ddfc0',
    recordList: [],
    recordKey: '',
    isTriggered: false,
    memberID: '',
    isLogin: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  clickToLogin() {
    nav.navigateTo('/login/login')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const memberID = local.getLocal('memberID');
    this.setData({
      memberID: memberID,
    })
    if (this.data.memberID == '' || this.data.memberID == '0'){
      this.setData({
        isLogin: false,
      })
    }else {
      this.setData({
        isLogin: true,
      })
      this.getPaipanitemsservice()
    }
  },

  bindKeyInput(e) {
    this.setData({
      recordKey: e.detail.value || '',
    })
  },

  /**
   * 获取搜索相关数据
   */
  handleToComfirm(event) {
    if (this.data.loading) return
    wx.pageScrollTo({
      scrollTop: 0
    })
    this.getPaipanitemsservice()
  },

  // /paipanitemsservice?param={"userInfo":{"user":"wx15a7538fa77ddfc0","eventCode":"311"},"paiPanItemsStuct":{"memberID":"27043","custName":""}}

  /**
   * 获取记录数据
   */
  getPaipanitemsservice() {
    var that = this;
    let userInfoDic = {
      "user": this.data.appid,
      "eventCode": "311"
    }
    let paiPanItemsStuctDic = {
      "memberID": this.data.memberID,
      "custName": this.data.recordKey,
    }
    let dic = {
      userInfo: userInfoDic,
      paiPanItemsStuct: paiPanItemsStuctDic,
    }
    console.log(dic)
    request.get('/paipanitemsservice', {param: dic}).then((res)=> {
      console.log(res)
      const returnCode = res.RETURN
      if (returnCode == 1) {
        that.setData({
          recordList: res.PAIPAN_LIST,
          isTriggered: false,
        })
      }
      
    })
  },

  /**
   * 点击跳转出险详情,传递本地数据
   */
  // onClickToInsureDetail(e) {
  //   const id = e.detail
  //   let currentDic = {}
  //   for(let i = 0; i < this.data.recordList.length; i ++) {
  //     let dic = this.data.recordList[i]
  //     if (dic.id == id) {
  //       currentDic = dic
  //     }
  //   }
  //   let jsonStr = JSON.stringify(currentDic)
  //   let data = encodeURIComponent(jsonStr)
  //   // nav.navigateTo('/insure/insure-detail/insure-detail?data='+data)
  // },
  

  refreshHandler() {
    // if (this.data.loading) return
    // this.setData({
    //   pageNo: 1,
    // })
    // this.getAccidentRecordList()
    this.getPaipanitemsservice()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // 页面被拉到底部
    // if (!this.data.isCompleted) {
    //   this.getAccidentRecordList()
    // }
  },

  onClickToRecordDetail(e) {
    const code = e.detail
    this.detailPaipanitemsservice(code)
  },

  detailPaipanitemsservice(code) {
    var that = this;
    let userInfoDic = {
      "user": this.data.appid,
      "eventCode":"312"
    }
    let paiPanItemsStuctDic = {
      "paiPanItemsCode": code,
      "memberID": this.data.memberID,
    }
    let dic = {
      userInfo: userInfoDic,
      paiPanItemsStuct: paiPanItemsStuctDic,
    }
    request.get('/paipanitemsservice',  {param: dic}).then((res)=> {
      console.log(res)
      const returnCode = res.RETURN
      if (returnCode == 1) {
        const object = res
        let jsonStr = JSON.stringify(object)
        let data = encodeURIComponent(jsonStr)
        nav.navigateTo('/detail/detail?data='+data)
      }
    })
  },

  onClickToRecordDelete(e) {
    const code = e.detail
    const that = this
    wx.showModal({
      title: '温馨提示',
      content: '是否删除?',
      complete: (res) => {
        if (res.cancel) {
        }
        if (res.confirm) {
          that.deletePaipanitemsservice(code)
        }
      }
    })
    
  },

  deletePaipanitemsservice(code) {
    var that = this;
    let userInfoDic = {
      "user": this.data.appid,
      "eventCode":"313"
    }
    let paiPanItemsStuctDic = {
      "paiPanItemsCode": code,
      "memberID": this.data.memberID
    }
    let dic = {
      userInfo: userInfoDic,
      paiPanItemsStuct: paiPanItemsStuctDic,
    }
    console.log(dic)
    request.get('/paipanitemsservice',  {param: dic}).then((res)=> {
      console.log(res)
      const returnCode = res.RETURN
      if (returnCode == 1) {
        that.getPaipanitemsservice()
      }
    })
  },


  
  
})